package com.surveillance.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.surveillance.BO.EtudiantBO;
import com.surveillance.BO.SalleBO;
import com.surveillance.entities.Salle;
import com.surveillance.util.HandlingDates;
import com.surveillance.util.LoadBeans;
import com.surveillance.util.SalleBis;
import com.surveillance.util.SalleBundle;
import com.surveillance.util.NiveauSpecialiteBundle;
import com.surveillance.util.SetOvSalles;
import com.surveillance.validators.AffectatonEtudiantsValidator1;
import com.surveillance.validators.AuthValidator;

@Controller
@SessionAttributes({ "availableSalles", "studentNumber", "salleBundle",
		"isMedcine", "niveau" })
public class MenuController {
	private AffectatonEtudiantsValidator1 affectatonEtudiantsValidator1;

	@Autowired
	public void setAffectatonEtudiantsValidator1(
			AffectatonEtudiantsValidator1 affectatonEtudiantsValidator1) {
		this.affectatonEtudiantsValidator1 = affectatonEtudiantsValidator1;
	}

	@RequestMapping(value = "/welcomePage", method = RequestMethod.GET)
	String renderWelcomePage(ModelMap model, HttpSession session) {
		if (session.getAttribute("userSession") != null) {
			return "welcomePage";
		} else {
			return "redirect:/";
		}
	}

	@RequestMapping(value = "/agenda", method = RequestMethod.GET)
	String renderAgendaView(ModelMap model) {

		return "welcomePage";
	}

	@RequestMapping(value = "/surveillants", method = RequestMethod.GET)
	String renderSurveillantsView(ModelMap model) {

		return "welcomePage";
	}

	@RequestMapping(value = "/affectationEtudiants", method = RequestMethod.GET)
	String renderStudentsAffectationsView(ModelMap model) {
		NiveauSpecialiteBundle niveauSpecialiteBundle = new NiveauSpecialiteBundle();
		model.addAttribute("niveauSpecialiteBundle", niveauSpecialiteBundle);
		return "affectationStep1";
	}

	@RequestMapping(value = "/affectationEtudiants", method = RequestMethod.POST)
	String renderingExamsRoomsView(
			@ModelAttribute("studentAffectationFirstStep") NiveauSpecialiteBundle studentAffectationFirstStep,
			BindingResult result, ModelMap model) {// IHM pour choisir les
		// examens est les salles

		affectatonEtudiantsValidator1.validate(studentAffectationFirstStep,
				result);
		if (result.hasErrors()) {
			return "affectationStep1";
		} else {
			SalleBO salleBO = (SalleBO) LoadBeans.getSalleBO();
			EtudiantBO etudiantBO = (EtudiantBO) LoadBeans.getEtudiantBO();
			ArrayList<Salle> availableSalles = new ArrayList<Salle>();
			availableSalles = new ArrayList(salleBO.getSallesDisponible());

			System.out.println("printing salles");
			for (int i = 0; i < availableSalles.size(); i++) {
				System.out.println(availableSalles.get(i));
			}
			
			SalleBundle salleBundle = new SalleBundle();
			int niveau = Integer.parseInt(studentAffectationFirstStep
					.getNiveau().trim());
			Boolean isMedcine =Boolean.parseBoolean(studentAffectationFirstStep.getSpecialite()) ;
			int studentNumber = etudiantBO.countStudentsByNiveauSpecialite(
					niveau, isMedcine);
			SetOvSalles setOvSalle = new SetOvSalles(availableSalles);
			model.addAttribute("availableSalles", setOvSalle);
			model.addAttribute("studentNumber", studentNumber);
			model.addAttribute("salleBundle", salleBundle);
			model.addAttribute("isMedcine", isMedcine);
			model.addAttribute("niveau", niveau);
			return "redirect:/affectationEtudiants-salle";
		}
	}
}
